package com.jilian.mydemo.modul.exam.ui;

import android.arch.lifecycle.Observer;
import android.arch.lifecycle.ViewModelProviders;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.jilian.mydemo.MyApplication;
import com.jilian.mydemo.R;
import com.jilian.mydemo.base.BaseActivity;
import com.jilian.mydemo.base.BaseDto;
import com.jilian.mydemo.common.dto.CommiteDto;
import com.jilian.mydemo.common.dto.QuestionDto;
import com.jilian.mydemo.common.vo.AwnserVo;
import com.jilian.mydemo.common.vo.ComminteListVo;
import com.jilian.mydemo.modul.main.viewmodel.MainViewModel;
import com.jilian.mydemo.modul.user.viewmodel.UserViewModel;
import com.jilian.mydemo.utils.EmptyUtils;
import com.jilian.mydemo.utils.ToastUitl;

import java.util.ArrayList;
import java.util.List;

public class AnswerQuestionsActivity extends BaseActivity {
    private TextView tvCount;
    private ProgressBar progressBar;
    private TextView tvProgress;
    private ImageView ivFinish;
    private TextView tvContent;
    private TextView tvA;
    private TextView tvB;
    private TextView tvC;
    private TextView tvD;
    private TextView tvOk;
    private UserViewModel viewModel;
    //当前显示的题目
    private int currentIndex = 1;
    //当前已经答的题目
    private int index = 0;
    private List<AwnserVo> lists;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        MyApplication.addActivity(this);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MyApplication.removeActivity(this);
    }

    @Override
    protected void createViewModel() {
        viewModel = ViewModelProviders.of(this).get(UserViewModel.class);
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_answerquestions;
    }

    @Override
    public void initView() {
        setNormalTitle("答题", v -> finish());
        tvCount = (TextView) findViewById(R.id.tv_count);
        progressBar = (ProgressBar) findViewById(R.id.progressBar);
        tvProgress = (TextView) findViewById(R.id.tv_progress);
        ivFinish = (ImageView) findViewById(R.id.iv_finish);
        tvContent = (TextView) findViewById(R.id.tv_content);
        tvA = (TextView) findViewById(R.id.tv_a);
        tvB = (TextView) findViewById(R.id.tv_b);
        tvC = (TextView) findViewById(R.id.tv_c);
        tvD = (TextView) findViewById(R.id.tv_d);
        tvOk = (TextView) findViewById(R.id.tv_ok);
        lists = new ArrayList<>();
    }

    @Override
    public void initData() {
        getQuestion();
    }

    private List<QuestionDto> list;

    private void getQuestion() {
        showLoadingDialog();
        viewModel.random();
        viewModel.getQuestionliveData().observe(this, new Observer<BaseDto<List<QuestionDto>>>() {
            @Override
            public void onChanged(@Nullable BaseDto<List<QuestionDto>> dto) {
                hideLoadingDialog();
                if (dto.isSuccess() && EmptyUtils.isNotEmpty(dto.getData())) {
                    list = dto.getData();
                    initExamView();
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }

    /**
     * 显示考试题目
     */
    private void initExamView() {
        if (currentIndex > list.size()) {
            ivFinish.setVisibility(View.VISIBLE);
            tvProgress.setVisibility(View.GONE);
            tvOk.setVisibility(View.VISIBLE);
            progressBar.setProgress(100);
            tvCount.setText(list.size() + "/" + list.size());
            tvA.setEnabled(false);
            tvB.setEnabled(false);
            tvC.setEnabled(false);
            tvD.setEnabled(false);
            return;

        }
        tvCount.setText((currentIndex - 1) + "/" + list.size());
        progressBar.setMax(list.size());
        progressBar.setProgress(currentIndex - 1);
        double progress = ((currentIndex - 1) / (Double.valueOf(list.size()))) * 100;
        tvProgress.setText((int) progress + "%");
        tvContent.setText(currentIndex + "." + list.get(currentIndex - 1).getContent());
        tvA.setText("A. " + list.get(currentIndex - 1).getAnswerA());
        tvB.setText("B. " + list.get(currentIndex - 1).getAnswerB());
        tvC.setText("C. " + list.get(currentIndex - 1).getAnswerC());
        tvD.setText("D. " + list.get(currentIndex - 1).getAnswerD());
        currentIndex++;

        tvA.setBackgroundResource(R.drawable.shape_dark_100);
        tvA.setTextColor(getResources().getColor(R.color.color_text));

        tvB.setBackgroundResource(R.drawable.shape_dark_100);
        tvB.setTextColor(getResources().getColor(R.color.color_text));

        tvC.setBackgroundResource(R.drawable.shape_dark_100);
        tvC.setTextColor(getResources().getColor(R.color.color_text));

        tvD.setBackgroundResource(R.drawable.shape_dark_100);
        tvD.setTextColor(getResources().getColor(R.color.color_text));


    }

    private void awnser(String str) {
        if (index == list.size()) {
            return;
        }
        AwnserVo vo = new AwnserVo();
        vo.setQuestionId(list.get(index).getId());
        vo.setSortNum(String.valueOf(index + 1));
        vo.setAnswer(str);
        lists.add(vo);
        index++;

    }

    @Override
    public void initListener() {
        tvOk.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                commite();

            }
        });
        tvA.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tvA.setBackgroundResource(R.drawable.shape_main_dark_100);
                tvA.setTextColor(getResources().getColor(R.color.color_main_title));
                tvA.setEnabled(false);
                tvB.setEnabled(false);
                tvC.setEnabled(false);
                tvD.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tvA.setEnabled(true);
                        tvB.setEnabled(true);
                        tvC.setEnabled(true);
                        tvD.setEnabled(true);
                        initExamView();
                        awnser("A");

                    }
                }, 300);
            }
        });
        tvB.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                tvB.setBackgroundResource(R.drawable.shape_main_dark_100);
                tvB.setTextColor(getResources().getColor(R.color.color_main_title));
                tvA.setEnabled(false);
                tvB.setEnabled(false);
                tvC.setEnabled(false);
                tvD.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tvA.setEnabled(true);
                        tvB.setEnabled(true);
                        tvC.setEnabled(true);
                        tvD.setEnabled(true);
                        initExamView();
                        awnser("B");

                    }
                }, 300);
            }
        });
        tvC.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                tvC.setBackgroundResource(R.drawable.shape_main_dark_100);
                tvC.setTextColor(getResources().getColor(R.color.color_main_title));
                tvA.setEnabled(false);
                tvB.setEnabled(false);
                tvC.setEnabled(false);
                tvD.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tvA.setEnabled(true);
                        tvB.setEnabled(true);
                        tvC.setEnabled(true);
                        tvD.setEnabled(true);
                        initExamView();
                        awnser("C");

                    }
                }, 300);
            }
        });
        tvD.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                tvD.setBackgroundResource(R.drawable.shape_main_dark_100);
                tvD.setTextColor(getResources().getColor(R.color.color_main_title));
                tvA.setEnabled(false);
                tvB.setEnabled(false);
                tvC.setEnabled(false);
                tvD.setEnabled(false);
                new Handler().postDelayed(new Runnable() {
                    @Override
                    public void run() {
                        tvA.setEnabled(true);
                        tvB.setEnabled(true);
                        tvC.setEnabled(true);
                        tvD.setEnabled(true);
                        initExamView();
                        awnser("D");

                    }
                }, 300);
            }
        });
    }

    private void commite() {
        showLoadingDialog();
        ComminteListVo vo = new ComminteListVo();
        vo.setLists(lists);
        viewModel.submit(vo);
        viewModel.getSubmitliveData().observe(this, new Observer<BaseDto<CommiteDto>>() {
            @Override
            public void onChanged(@Nullable BaseDto<CommiteDto> dto) {
                hideLoadingDialog();
                if (dto.isSuccess()) {
                    Intent intent = new Intent(AnswerQuestionsActivity.this, ExamResultActivity.class);
                    CommiteDto commiteDto = dto.getData();
                    intent.putExtra("data", commiteDto);
                    startActivity(intent);
                    finish();
                } else {
                    ToastUitl.showImageToastTips(dto.getMsg());
                }
            }
        });
    }


}
